<template>
  <div class="form">
    <el-button type="primary" @click="goback">返回</el-button>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
      size="small">
      <el-form-item label="用户名" prop="userName">
        <el-input type="text" v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.Password" autocomplete="new-password" clearable></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input type="text" v-model="ruleForm.RealName"></el-input>
      </el-form-item>
      <el-form-item label="部门编号" prop="DeptID">
        <el-input v-model.number="ruleForm.DeptID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addFun } from '@/CommonJs/RequestApi';

export default {
  name: "AddUser",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.Password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userName: '',
        Password: '',
        RealName: '',
        DeptID: '',
        checkPass: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    };
  },
  components: {

  }, methods: {

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          delete this.ruleForm.checkPass

          addFun('user/add_user', {
            UserName: this.ruleForm.userName,
            Password: this.ruleForm.Password,
            RealName: this.ruleForm.RealName,
            DeptID: this.ruleForm.DeptID,
          }).then((response) => {
            if (response.data.state == 200) {
              alert('添加用户成功');
            } else {
              alert('用户添加失败')
            }
          })




          // this.$http({
          //   url: 'http://localhost:4444/ssm_his/his/user/addUser', method: 'post',
          //   params: {
          //     UserName: this.ruleForm.userName,
          //     Password: this.ruleForm.Password,
          //     RealName: this.ruleForm.RealName,
          //     DeptID: this.ruleForm.DeptID,
          //   }
          // }).then((response) => {
          //   if (response.data.state == 200) {
          //     alert('添加用户成功');
          //   } else {
          //     alert('用户添加失败')
          //   }
          // })

        } else {
          alert('部门编号数字')
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goback() {
      this.$router.push('/workbench')
    }
  }

}
</script>



<style scoped>.form {
  width: 500px;
}</style>